/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceLoginServer;

import seGobangServer.DBController.UserController;
import seGobangServer.Model.User;

/**
 *
 * @author Peach
 */
public class LoginServer {
    
    public int TestLink(){
    
        return 1;
    }
    
    public int login(String detail,String userId,String password){
        User user=User.getInstance();
        user.SetID(userId);
        UserController controller=new UserController();
        System.out.println("Detail:"+controller.GetUser(user).getDetail());
        System.out.println("Password:"+controller.GetUser(user).getPassword());
        if((controller.GetUser(user).getDetail()).equalsIgnoreCase("0")&&
                controller.GetUser(user).getPassword().equalsIgnoreCase(password)){
            user.SetDetail(getDetail());
            controller.SetDetail(user);
            return Integer.parseInt(user.getDetail());
        }else if((controller.GetUser(user).getDetail()).
                equalsIgnoreCase(detail)){
            return Integer.parseInt(detail);
        }else{
            return 0;
        }
    }
    
    public int logout(String userId){
        User user=User.getInstance();
        user.SetID(userId);
        UserController controller=new UserController();
        if((controller.GetUser(user).getDetail()).equalsIgnoreCase("0")){
            return 0;
        }else{
            int detail=Integer.parseInt(controller.GetUser(user).getDetail());
            controller.ReleaseDetail(user);
            return detail;
        }
    }
    
    private String getDetail(){
        int random=(int)(Math.random()*100000);
        if(random<90000){
            random+=10000;
        }
        return random+"";
    }
    
    public static void main(String[] args){
        LoginServer test=new LoginServer();
        test.logout("1");
    }
}
